
//
//  DDWelcomVC.swift
//  SinaMicroBlog
//
//  Created by dqw on 15/7/31.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import SDWebImage

class DDWelcomVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        loadAndLayoutSubViews()
        if let avatar = DDUserAccount.aUserAccount?.avatar_large {
            icon.sd_setImageWithURL([NSURL(string: avatar)])
        }
    }
    // TODO: 此处有很多知识点。
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconBottomCons = UIScreen.mainScreen().bounds.height - iconBottomCons!.constant
        
        UIView .animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 8, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.setNeedsDisplay()
            }) { (_) -> Void in
                //TODO: 注册通知
        }
        
    }
    var iconBottomCons: NSLayoutConstraint?
    
    func loadAndLayoutSubViews() {
        
        
        view.addSubview(backImg)
        view.addSubview(icon)
//        view.addSubview(startBtn)
        view.addSubview(messageLab)
        
        backImg.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(H:|-0-[subview]-0-|, options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview":backImg]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(V:|-0-[subview]-0-|, options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview":backImg]))
        
        icon.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: icon, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: icon, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: icon, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: icon, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 160))
        // 记录底边约束
        iconBottomCons = view.constraints.last
        
        messageLab.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: messageLab, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: messageLab, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
    }
    
    lazy var backImg = UIImageView(image: UIImage(named: "ad_background"))
    lazy var icon:UIImageView = {
        let iconT = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconT.layer.masksToBounds = true
        iconT.layer.cornerRadius = 45
        return iconT
        
    }()
    lazy var messageLab: UILabel = {
       let lable = UILabel()
        lable.text = "欢迎归来"
        lable.sizeToFit()
        return lable
        
    }()
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
